//
//  DeviceContinueModel.m
//
//  Created by  殷聃 on 2019/11/3
//  Copyright (c) 2019 yek. All rights reserved.
//

#import "DeviceContinueModel.h"


NSString *const kDeviceContinueModelStatus = @"status";
NSString *const kDeviceContinueModelDeviceId = @"deviceId";
NSString *const kDeviceContinueModelEndTime = @"endTime";
NSString *const kDeviceContinueModelIsOnlie = @"isOnlie";
NSString *const kDeviceContinueModelBeganTime = @"beganTime";
NSString *const kDeviceContinueModelLastTime = @"lastTime";
NSString *const kDeviceContinueModelProwerNumber = @"prowerNumber";


@interface DeviceContinueModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation DeviceContinueModel

@synthesize status = _status;
@synthesize deviceId = _deviceId;
@synthesize endTime = _endTime;
@synthesize isOnlie = _isOnlie;
@synthesize beganTime = _beganTime;
@synthesize lastTime = _lastTime;
@synthesize prowerNumber = _prowerNumber;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.status = [self objectOrNilForKey:kDeviceContinueModelStatus fromDictionary:dict];
            self.deviceId = [self objectOrNilForKey:kDeviceContinueModelDeviceId fromDictionary:dict];
            self.endTime = [[self objectOrNilForKey:kDeviceContinueModelEndTime fromDictionary:dict] doubleValue];
            self.isOnlie = [[self objectOrNilForKey:kDeviceContinueModelIsOnlie fromDictionary:dict] boolValue];
            self.beganTime = [[self objectOrNilForKey:kDeviceContinueModelBeganTime fromDictionary:dict] doubleValue];
            self.lastTime = [[self objectOrNilForKey:kDeviceContinueModelLastTime fromDictionary:dict] doubleValue];
            self.prowerNumber = [[self objectOrNilForKey:kDeviceContinueModelProwerNumber fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.status forKey:kDeviceContinueModelStatus];
    [mutableDict setValue:self.deviceId forKey:kDeviceContinueModelDeviceId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.endTime] forKey:kDeviceContinueModelEndTime];
    [mutableDict setValue:[NSNumber numberWithBool:self.isOnlie] forKey:kDeviceContinueModelIsOnlie];
    [mutableDict setValue:[NSNumber numberWithDouble:self.beganTime] forKey:kDeviceContinueModelBeganTime];
    [mutableDict setValue:[NSNumber numberWithDouble:self.lastTime] forKey:kDeviceContinueModelLastTime];
    [mutableDict setValue:[NSNumber numberWithDouble:self.prowerNumber] forKey:kDeviceContinueModelProwerNumber];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.status = [aDecoder decodeObjectForKey:kDeviceContinueModelStatus];
    self.deviceId = [aDecoder decodeObjectForKey:kDeviceContinueModelDeviceId];
    self.endTime = [aDecoder decodeDoubleForKey:kDeviceContinueModelEndTime];
    self.isOnlie = [aDecoder decodeBoolForKey:kDeviceContinueModelIsOnlie];
    self.beganTime = [aDecoder decodeDoubleForKey:kDeviceContinueModelBeganTime];
    self.lastTime = [aDecoder decodeDoubleForKey:kDeviceContinueModelLastTime];
    self.prowerNumber = [aDecoder decodeDoubleForKey:kDeviceContinueModelProwerNumber];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_status forKey:kDeviceContinueModelStatus];
    [aCoder encodeObject:_deviceId forKey:kDeviceContinueModelDeviceId];
    [aCoder encodeDouble:_endTime forKey:kDeviceContinueModelEndTime];
    [aCoder encodeBool:_isOnlie forKey:kDeviceContinueModelIsOnlie];
    [aCoder encodeDouble:_beganTime forKey:kDeviceContinueModelBeganTime];
    [aCoder encodeDouble:_lastTime forKey:kDeviceContinueModelLastTime];
    [aCoder encodeDouble:_prowerNumber forKey:kDeviceContinueModelProwerNumber];
}

- (id)copyWithZone:(NSZone *)zone
{
    DeviceContinueModel *copy = [[DeviceContinueModel alloc] init];
    
    if (copy) {

        copy.status = [self.status copyWithZone:zone];
        copy.deviceId = [self.deviceId copyWithZone:zone];
        copy.endTime = self.endTime;
        copy.isOnlie = self.isOnlie;
        copy.beganTime = self.beganTime;
        copy.lastTime = self.lastTime;
        copy.prowerNumber = self.prowerNumber;
    }
    
    return copy;
}


@end
